# coding:utf-8

from .preprocessor import Preprocessor


class FillNa(Preprocessor):
    # mean,median,mode,dropna
    methods = []
    code = ""

    def __init__(self):
        Preprocessor.__init__(self)
        self.algorithm_name = "缺失值"
        self.ipynb_template_name = "fillna-template.ipynb"

    def implent(self): 
        Preprocessor.implent(self)
        tmp = []
        for i in range(len(self.subset)):
            name = self.subset[i]
            method = self.methods[i]
            if method == "dropna":
                self.new_data.dropna(subset=[name], inplace=True)
                tmp.append("new_data.dropna(subset=['%s'], inplace=True)" % (name))
            elif method == "mean":
                self.new_data.fillna(value={name:self.new_data[name].mean()}, inplace=True)
                tmp.append("new_data.fillna(value={'%s':new_data['%s'].mean()}, inplace=True)" % (name, name))
            elif method == "median":
                self.new_data.fillna(value={name:self.new_data[name].median()}, inplace=True)
                tmp.append("new_data.fillna(value={'%s':new_data['%s'].median()}, inplace=True)" % (name, name))
            elif method == "mode":
                self.new_data.fillna(value={name:self.new_data[name].mode()[0]}, inplace=True)
                tmp.append("new_data.fillna(value={'%s':new_data['%s'].mode()[0]}, inplace=True)" % (name, name))
            else:
                continue
        self.code = "\n".join(tmp)

    def prepareIpynbItems(self):
        Preprocessor.prepareIpynbItems(self)
        self.ipynb_items["#code#"] = self.code
